<%-- 
    Document   : searchquery
    Created on : Nov 17, 2012, 4:49:59 PM
    Author     : Ahsan
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="ca.trader.beans.java.SalesSearchBean"%>
<%@page import="java.net.URLEncoder"%>
<%@ taglib uri="/WEB-INF/tlds/PrintSaleListTable.tld" prefix="saletable" %>
<%@ taglib uri="/WEB-INF/tlds/PrintPageNumbers.tld" prefix="printpages" %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <jsp:include page="head.jsp" />
        <title>JSP Page</title>
    </head>
    <body>
	<c:choose>
	    <c:when test="${empty param.page}" >
		Missing parameter "page".
	    </c:when>
	    <c:otherwise>
		<jsp:useBean id="sales" class="ca.trader.beans.java.SalesSearchBean" scope="request" />
		<jsp:setProperty name="sales" property="pageNumber" param="page"/>
		<%
				  String redirectUrl = "searchquery.jsp?page=%d";
		%>
		<c:choose>
		    <c:when test="${!(empty param.keywords)}" >
			<jsp:setProperty name="sales" property="keywords" param="keywords"/>
			<%
						redirectUrl += "&keywords=" + URLEncoder.encode(request.getParameter("keywords"), "UTF-8");
			%>
		    </c:when>
		</c:choose>
		<c:choose>
		    <c:when test="${!(empty paramValues.categories)}" >
			<%
						sales.setCategories(request.getParameterValues("categories"));
						for (String category : request.getParameterValues("categories"))
						{
							 redirectUrl += "&categories=" + URLEncoder.encode(category, "UTF-8");
						}
			%>
		    </c:when>
		</c:choose>
		<c:choose>
		    <c:when test="${!(empty param.country)}" >
			<jsp:setProperty name="sales" property="country" param="country"/>
			<%
						redirectUrl += "&country=" + URLEncoder.encode(request.getParameter("country"), "UTF-8");
			%>
			<c:choose>
			    <c:when test="${!(empty param.region)}" >
				<jsp:setProperty name="sales" property="region" param="region"/>
				<%
							   redirectUrl += "&region=" + URLEncoder.encode(request.getParameter("region"), "UTF-8");
				%>
				<c:choose>
				    <c:when test="${!(empty param.city)}" >
					<jsp:setProperty name="sales" property="city" param="city"/>
					<%
								      redirectUrl += "&city=" + URLEncoder.encode(request.getParameter("city"), "UTF-8");
					%>
				    </c:when>
				</c:choose>
			    </c:when>
			</c:choose>
		    </c:when>
		</c:choose>
		<%
				  sales.searchResults();
		%>
		<c:choose>

		    <c:when test="${!sales.resultsFound}">
			<jsp:getProperty name="sales" property="errorMessage"/> 

		    </c:when>
		    <c:otherwise>
			<saletable:PrintSaleListTable saleList="${sales.results}" />
			<br/>
			<printpages:PrintPageNumbers currentPageNumber="${param.page}" totalPageNumbers="${sales.totalCount}" 
			redirectOutputUrlPattern="<%= redirectUrl%>" />
		    </c:otherwise>
		</c:choose>
	    </c:otherwise>
	</c:choose>
    </body>
</html>
